package com.shantoo.databinding;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.retrofitutils.ItheimaHttp;
import com.itheima.retrofitutils.Request;
import com.itheima.retrofitutils.listener.HttpResponseListener;
import com.shantoo.databinding.bean.BaseResponse;
import com.shantoo.databinding.bean.Category;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DataBindAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new DataBindAdapter(R.layout.item_category,null);

        recyclerView.setAdapter(adapter);
        getCategory();

        CategoryPresenter presenter = new CategoryPresenter();
    }

    public void getName(DataBindAdapter.DataBindHolder holder){
        TextView textView = holder.getView(R.id.gc_id);
        Log.e("MainActivity", "info:" + textView.getText());
    }

    //获取商品一级分类
    public void getCategory() {
        Request request = ItheimaHttp.newPostRequest("v1/goods/top-category");
        ItheimaHttp.send(request, new HttpResponseListener<BaseResponse<Category>>() {
            @Override
            public void onResponse(BaseResponse<Category> response) {
                if (response.isState()) {
                    List<Category> list = response.getData();
                    adapter.setNewData(list);
                } else {
                    Toast.makeText(getApplicationContext(), response.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
